/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ihale.yonetici;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.ihale.Ihale;
import org.ihale.database.Database;
import org.ihale.satinalmaci.SatinAlmaci;

/**
 *
 * @author Mehmet Sinan
 */
@ManagedBean(name = "adminBean")
@ViewScoped

public class AdminBean {

    // Değişkenler için alan başlangıcı
    private List<Ihale> ihaleler = new LinkedList<Ihale>();
    private static Ihale seciliIhale = new Ihale();
    private SatinAlmaci satinAlmaci;
    private Map<String, String> satinAlmacilar = new HashMap<String, String>();
    private Admin admin;
    private Ihale ihale;
    private String satinAlmaciId;
    

    public AdminBean() {
        try {
            admin = new Admin();
            ihale = new Ihale();
            satinAlmaci = new SatinAlmaci();
            Database db = new Database();
            db.connect();
            satinAlmacilar = db.listeleSatinAlmacilarMap();
            ihaleler = db.listeleIhale();

            db.disconnect();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Değişkenler için alan bitişi
    // Getter-Setter için alan başlangıcı
    public List<Ihale> getIhaleler() {
        return ihaleler;
    }

    public void setIhaleler(List<Ihale> ihaleler) {
        this.ihaleler = ihaleler;
    }

    public Ihale getSeciliIhale() {
        return seciliIhale;
    }

    public String getSatinAlmaciId() {
        return satinAlmaciId;
    }

    public void setSatinAlmaciId(String satinAlmaciId) {
        this.satinAlmaciId = satinAlmaciId;
    }

    public Ihale getIhale() {
        return ihale;
    }

    public void setIhale(Ihale ihale) {
        this.ihale = ihale;
    }

    public void setSeciliIhale(Ihale seciliIhale) {
        AdminBean.seciliIhale = seciliIhale;
    }

    public SatinAlmaci getSatinAlmaci() {
        return satinAlmaci;
    }

    public void setSatinAlmaci(SatinAlmaci satinAlmaci) {
        this.satinAlmaci = satinAlmaci;
    }

    public Map<String, String> getSatinAlmacilar() {
        return satinAlmacilar;
    }

    public void setSatinAlmacilar(Map<String, String> satinAlmacilar) {
        this.satinAlmacilar = satinAlmacilar;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }
    
    // Getter-Setter için alan bitişi
    // Fonksiyonlar için alan başlangıcı

    // Fonksiyonlar için alan bitişi

    public void ekleIhale() {
        try {
            satinAlmaci.setId(Integer.parseInt(satinAlmaciId));
            ihale.setSatinAlmaci(satinAlmaci);
            String[] tmp = new String[2];
            tmp = ihale.getBaslangicSaati().split(":");
            String[] tmp2 = new String[2];
            tmp2 = ihale.getBitisSaati().split(":");
            Database db = new Database();
            db.connect();
            db.ekleIhale(ihale);
            db.disconnect();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("İhale eklendi"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }






    }

    public void silIhale() {
        try {
            Database db = new Database();
            db.connect();
            db.silIhale(seciliIhale);
            ihaleler = db.listeleIhale();
            db.disconnect();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("ihale silindi"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    public void guncelleIhale() {
        try {
            Database db = new Database();
            db.connect();
            seciliIhale.getSatinAlmaci().setId(Integer.parseInt(satinAlmaciId));
            db.guncelleIhale(seciliIhale);
            db.disconnect();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("İhale güncellendi"));
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
